/**
 * @Title: DatbSelectOrganByIdSyncLogic.java
 * @Package: com.jason.fingerprint.logic
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月21日 上午9:54:27
 * @Version: V1.0
 */
package com.jason.fingerprint.logic;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.GsonBuilder;
import com.jason.fingerprint.AppContext;
import com.jason.fingerprint.AppGlobal;
import com.jason.fingerprint.beans.BaseBean;
import com.jason.fingerprint.common.UIHelper;
import com.jason.fingerprint.configs.HttpConfig;
import com.jason.fingerprint.utils.CodecUtils;

import org.kymjs.aframe.utils.StringUtils;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * @ClassName: DatbSelectOrganByIdSyncLogic
 * @Description: 判断当前机构是否有权限录入指纹信息
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月30日16:56:16
 */
public class DatbSelectOrganByIdSyncLogic extends Logic {

	private static final String TAG = "DatbSelectOrganByIdSyncLogic";

	private Handler mHandler;
	private boolean mIsCallBack = false;// 是否有回调，此需要Handle,默认不回调
	private Message mMessage;

	public DatbSelectOrganByIdSyncLogic() {

	}

	public DatbSelectOrganByIdSyncLogic(AppContext appContext, Handler handler) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
	}

	public DatbSelectOrganByIdSyncLogic(AppContext appContext, Handler handler,
			boolean isCallBack) {
		super(appContext);
		this.mHandler = handler;
		this.mMessage = new Message();
		this.mIsCallBack = isCallBack;
	}

	@Override
	public void execute() {
		datbRecordSync();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jason.fingerprint.logic.Logic#send()
	 */
	@Override
	public void send() {
		if (mIsCallBack) {
			mHandler.sendMessage(mMessage);
			mMessage = new Message();
		}

	}

	// 档案同步
	@SuppressLint("LongLogTag")
	private void datbRecordSync() {
		mParams = new RequestParams();
		mParams.addFormDataPart(AppGlobal.HTTP_PARAM_IMEI, mImei);
		mParams.addFormDataPart(AppGlobal.HTTP_PARAM_ORGANID, mMachId);
		String url = HttpConfig.POST_URL_DATB_SELECTORGANBYID;
		Log.i(TAG, "----->API:" + url);
		HttpRequest.post(url, mParams, new BaseHttpRequestCallback<String>() {

			@Override
			protected void onSuccess(String o) {
				super.onSuccess(o);
				try{
					if (o == null){
						mMessage.what = 2;
						send();
						return;
					}
					String result = CodecUtils.base64Decode(o);
					Log.i(TAG, "----->Result:" + result);
					BaseBean bean = new GsonBuilder().create().fromJson(result, BaseBean.class);
					if (bean != null && bean.isSuccess()) {
						String errorMessage = bean.getRespMsg();
						if (!StringUtils.isEmpty(errorMessage) && errorMessage.equals("有录入权限!")) {
							mMessage.what = 0;
							send();
						}else {
							mMessage.what = 1;
							mMessage.obj = errorMessage;
							send();
						}
					}else {
						mMessage.what = 2;
						send();
					}
				}catch (Exception e){
					mMessage.what = 2;
					send();
				}
			}

			@Override
			public void onFailure(int errorCode, String msg) {
				super.onFailure(errorCode, msg);
				Log.i(TAG, "----->Result:arg1:" + errorCode + ",arg2:" + msg);
				if (errorCode == UIHelper.HTTP_CODE_TIMEOUT){
					mMessage.what = UIHelper.HTTP_CODE_TIMEOUT;
				}else {
					mMessage.what = 3;
				}
				send();
			}
		});
	}

	/* (non-Javadoc)
	 * @see com.jason.fingerprint.logic.Logic#destory()
	 */
	@Override
	public void destory() {
		// TODO Auto-generated method stub
		
	}

}
